package com.djf.eg.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.djf.eg.exception.DRuntimeException;
import com.djf.eg.exception.DSecurityException;
import com.djf.eg.result.Result;
import com.djf.eg.result.ResultCode;

@Controller
public class DBaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(DBaseController.class);
	
	@ExceptionHandler(DSecurityException.class)
	@ResponseBody
	public Result catchDRuntimeException(DSecurityException exception,
			HttpServletRequest request, HttpServletResponse response) {
		logger.error("[D SecurityException]:{} {}", exception.getCode(),
				exception.getMessage());
		logger.error("",exception);
		return Result.failure(ResultCode.PERMISSION_NO_ACCESS);
	}
	
	@ExceptionHandler(DRuntimeException.class)
	@ResponseBody
	public Result catchDRuntimeException(DRuntimeException exception,
			HttpServletRequest request, HttpServletResponse response) {
		logger.error("[D RuntimException]:{} {}", exception.getCode(),
				exception.getMessage());
		logger.error("",exception);
		return Result.failure(ResultCode.SYSTEM_RUNTIME_ERROR);
	}
	
}
